package com.zhou.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhou.api.dto.WareHouseExamineDTO;
import com.zhou.api.entity.Inventory;
import com.zhou.api.entity.WareHouse;
import com.zhou.mapper.WareHouseMapper;
import com.zhou.result.R;
import com.zhou.service.InventoryService;
import com.zhou.service.WareHouseService;
import com.zhou.utils.APPApplicationException;
import com.zhou.utils.BeanChangeUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.zhou.utils.CommonCast.NO;
import static com.zhou.utils.CommonCast.YES;

/**
 * @Description : 仓库管理服务层
 * @Author : zyy
 * @Date: 2024/7/25  9:20
 */
@Service
public class WareHouseServiceImpl extends ServiceImpl<WareHouseMapper, WareHouse> implements WareHouseService {

    @Resource
    private InventoryService inventoryService;
    /**
     * @param goods
     * @param nums
     * @return
     */
    @Override
    public String add(String goods, Integer nums) {
        if (goods != null && nums != null) {
            if (nums > NO) {
                WareHouse wareHouse = new WareHouse();
                wareHouse.setGoods(goods);
                wareHouse.setNums(nums);
                boolean save = this.save(wareHouse);
                if (save) {
                    return IdWorker.getIdStr();
                }else {
                    return null;
                }
            }
        }
        return "参数传参错误请重试";
    }

    /**
     * @param id
     * @param goods
     * @param nums
     * @return
     */
    @Override
    public String update(String id, String goods, Integer nums) {
        if (id != null && goods != null && nums != null) {
            if (BeanChangeUtils.isEmpty(this.getById(id))) {
                throw new APPApplicationException("该物品不存在");
            }
            if (nums >= NO) {
                WareHouse wareHouse = new WareHouse();
                wareHouse.setId(id);
                wareHouse.setGoods(goods);
                wareHouse.setNums(nums);
                boolean update = this.updateById(wareHouse);
                if (update) {
                    return "修改成功";
                }
            }
        }

        return "修改失败";
    }

    /**
     * @param id
     * @return 真删除
     */
    @Override
    public String delete(String id) {
        WareHouse byId = this.getById(id);
        if (BeanChangeUtils.isEmpty(byId)) {
            throw new APPApplicationException("没有查询到数据，请重试");
        }
        if (this.removeById(id)) {
            return "删除成功";
        }
        return "删除失败";
    }

    /**
     * 软删除
     * @param id
     * @return
     */
    @Override
    public String updateRemove(String id) {
        WareHouse wareHouse = this.getById(id);
        if (BeanChangeUtils.isEmpty(wareHouse)) {
            throw new APPApplicationException("没有查询到要删除的数据，请重试");
        }
        wareHouse.setDelFlag(YES);
        boolean b = this.updateById(wareHouse);
        if (b) {
            return "删除成功";
        }
        return "删除失败";
    }

    /**
     * @param page
     * @param status
     * @param goods
     * @return
     */
    @Override
    public R<Page<WareHouse>> getList(Page page, String status, String goods) {
        LambdaQueryWrapper<WareHouse> wrapper = new LambdaQueryWrapper<>();
        if (BeanChangeUtils.isNotEmpty(status)) {
            wrapper.eq(WareHouse::getStatus, status);
        }
        if (BeanChangeUtils.isNotEmpty(goods)) {
            wrapper.like(WareHouse::getGoods, goods);
        }

        return R.success(baseMapper.selectPage(page, wrapper));
    }

    /**
     * 审核
     * @param dto
     * @return
     */
    @Override
    public String examine(WareHouseExamineDTO dto) {
        String[] split = dto.getInventoryId().split(",");
        List<Inventory> list = new ArrayList<>();
        if(BeanChangeUtils.isNotEmpty(split)) {
            //有分析出来的id
            for (String s : split) {
                Inventory inventory = inventoryService.examine(s);
                if (BeanChangeUtils.isEmpty(inventory)) {
                    throw new APPApplicationException("有个待审核的记录错误请刷新页面重试");
                }
                inventory.setAuditStatus(dto.getAuditStatus());
                list.add(inventory);
            }
            inventoryService.updateList(list);
            return "审核完成";
        }
        return null;
    }
}
